package com.acwing.partition36;

import java.io.*;

/**
 * @author rkc
 * @date 2022/7/20 9:49
 */
public class AC3540二叉搜索树 {

    private static final int N = 110;
    private static int[] l = new int[N], r = new int[N], w = new int[N];
    private static int root = 0, n, idx = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        for (int i = 0; i < n; i++) {
            int x = nextInt();
            root = add(root, x);
        }
        for (int i = 0; i < 3; i++) {
            dfs(root, i);
            writer.println();
        }
        writer.flush();
    }

    private static void dfs(int u, int opt) {
        if (u == 0) return;
        if (opt == 0) writer.print(w[u] + " ");
        dfs(l[u], opt);
        if (opt == 1) writer.print(w[u] + " ");
        dfs(r[u], opt);
        if (opt == 2) writer.print(w[u] + " ");
    }

    private static int add(int u, int x) {
        if (u == 0) {
            w[++idx] = x;
            return idx;
        } else if (x < w[u]) {
            l[u] = add(l[u], x);
        } else if (x > w[u]) {
            r[u] = add(r[u], x);
        }
        return u;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
